metric_names <- c("ACC", "$F_{1}$")
for (k in 1:2) {
  # --------------------------------------
  # idx: 1 准确率，2 F1 分数
  # num_metric: 指标的个数
  # num_dataset: 跑的数据集的数量
  idx_metric <- k
  num_metric <- 1
  num_dataset <- 13
  num_model <- 6
  kernel_type <- "rbf"
  #---------------------------------------
  model_names <- c("Hinge-TSVM", "SHinge-SVM", "LS-SVM",
                   "CL2p-TSVM", "C-TSVM", "BLS-BSH-SVM")
  data_path <- paste("../results-", kernel_type, "-classification/", sep = "")
  list <- list.files(path = data_path)
  list
  dataset_names <- c()
  for (i in 1:length(list)) {
    name_format <- strsplit(list[i], "_")[[1]][3]
    name_temp <- strsplit(name_format, "[.]")[[1]][1]
    dataset_names <- append(dataset_names, name_temp)
  }
  
  data_metric <- data.frame()
  data_std <- data.frame()
  
  for (i in list) {
    path <- i
    data_i <- t(read.csv(file = paste(data_path, path, sep = ""),
                         check.names = FALSE, header = TRUE,
                         row.names = 1))
    data_metric <- rbind(data_metric, data_i[idx_metric, ])
    data_std    <- rbind(data_std, data_i[num_metric+idx_metric, ])
  }
  data_metric <- as.matrix(data_metric)
  data_std <- as.matrix(data_std)
  colnames(data_metric) <- model_names
  colnames(data_std) <- model_names
  # print(data_metric)
  # print(data_std)
  
  avg_rank <- function(data_metric) {
    apply(data_metric, 1, rank)
  }
  print('--------------------------------------------------')
  
  mean_metric_0 <- apply(data_metric[1:num_dataset, ], 2, mean)
  data_avg_metric_0 <- round(mean_metric_0, 3)
  data_avg_metric_str_0 <- format(data_avg_metric_0, nsmall = 3)
  data_avg_metric_str_0 <- paste("&", data_avg_metric_str_0, sep = "", collapse = "")
  data_avg_metric_str_0 <- paste("Average", metric_names[k], data_avg_metric_str_0, "\\\\ \n")
  print(friedman.test(data_metric[1:num_dataset, ]))
  ChiF <- friedman.test(data_metric[1:num_dataset, ])
  ChiF <- ChiF$statistic
  FF <- ((num_dataset - 1)*ChiF)/(num_dataset*(num_model - 1) - ChiF)
  cat("Chi-square", format(round(ChiF, 3), nsmall = 3), "\n")
  cat("F", format(round(FF, 3), nsmall = 3), "\n")
  
  rank_row_0 <- t(apply(-data_metric[1:num_dataset, ], 1, rank))
  data_avg_rank_0 <- round(apply(rank_row_0, 2, mean), 3)
  data_avg_rank_str_0 <- format(data_avg_rank_0, nsmall = 3)
  data_avg_rank_str_0 <- paste("&", data_avg_rank_str_0, sep = "", collapse = "")
  data_avg_rank_str_0 <- paste("Average Rank", data_avg_rank_str_0, "\\\\ \n")
  print(data_avg_rank_str_0)
  print(data_avg_rank_0[1:5] - data_avg_rank_0[6])
  
  mean_metric_25label <- apply(data_metric[(num_dataset+1):(2*num_dataset), ], 2, mean)
  data_avg_metric_25label <- round(mean_metric_25label, 3)
  data_avg_metric_str_25label <- format(data_avg_metric_25label, nsmall = 3)
  data_avg_metric_str_25label <- paste("&", data_avg_metric_str_25label, sep = "", collapse = "")
  data_avg_metric_str_25label <- paste("Average", metric_names[k], data_avg_metric_str_25label, "\\\\ \n")
  print(friedman.test(data_metric[(num_dataset+1):(2*num_dataset), ]))
  ChiF <- friedman.test(data_metric[(num_dataset+1):(2*num_dataset), ])
  ChiF <- ChiF$statistic
  FF <- ((num_dataset - 1)*ChiF)/(num_dataset*(num_model - 1) - ChiF)
  cat("Chi-square", format(round(ChiF, 3), nsmall = 3), "\n")
  cat("F", format(round(FF, 3), nsmall = 3), "\n")
  
  rank_row_25label <- t(apply(-data_metric[(num_dataset+1):(2*num_dataset), ], 1, rank))
  data_avg_rank_25label <- round(apply(rank_row_25label, 2, mean), 3)
  data_avg_rank_str_25label <- format(data_avg_rank_25label, nsmall = 3)
  data_avg_rank_str_25label <- paste("&", data_avg_rank_str_25label, sep = "", collapse = "")
  data_avg_rank_str_25label <- paste("Average Rank", data_avg_rank_str_25label, "\\\\ \n")
  print(data_avg_rank_str_25label)
  print(data_avg_rank_25label[1:5] - data_avg_rank_25label[6])

  data_metric <- round(data_metric, 3)
  data_std <- round(data_std, 3)
  
  n_data <- nrow(data_metric)
  n_model <- length(model_names)
  data_str <- matrix(0, n_data, n_model)
  
  for (i in 1:n_data) {
    idx_max <- which(data_metric[i, ] == max(data_metric[i, ]))
    for (j in 1:n_model) {
      if (any(j == idx_max)) {
        str_temp <- paste(format(data_metric[i, j], nsmall = 3),
                          "$\\pm$",
                          format(data_std[i, j], nsmall = 3), sep = "")
        data_str[i, j] <- paste("\\textbf{", str_temp, "}", sep = "")
      } else {
        data_str[i, j] <- paste(format(data_metric[i, j], nsmall = 3),
                                "$\\pm$",
                                format(data_std[i, j], nsmall = 3), sep = "")
      }
    }
  }
  
  data_str <- cbind(dataset_names, data_str)
  data_str_single_str <- paste("\\toprule \n",
                               "\\multicolumn{7}{c}{(a) without noise} \\\\ \n",
                               "\\midrule \n",
                               "&Hinge-TSVM&SHinge-TSVM&LS-TSVM&C$L_{2,p}$-TSVM&C-TSVM&BLS-BSH-TSVM\\\\ \n",
                               "&", paste(rep(paste(metric_names[k], "$\\pm$", "sd"), 6), collapse = "&"), "\\\\ \n",
                               "\\midrule \n", sep = "")
  for (i in 1:n_data) {
    if (i == num_dataset + 1) {
      
      data_str_single_str <- paste(data_str_single_str, "\\midrule \n",
                                   "\\multicolumn{7}{c}{(b) $25\\%$ label noise} \\\\ \n",
                                   "\\midrule \n",
                                   "&Hinge-TSVM&SHinge-TSVM&LS-TSVM&C$L_{2,p}$-TSVM&C-TSVM&BLS-BSH-TSVM\\\\ \n",
                                   "&", paste(rep(paste(metric_names[k], "$\\pm$", "sd"), 6), collapse = "&"), "\\\\ \n",
                                   "\\midrule \n", sep = "")
    }
    for (j in 1:(n_model+1)) {
      if (j == 1) {
        data_str_single_str <- paste(data_str_single_str, data_str[i, j], sep = "")
      } else if (j < (n_model+1)){
        data_str_single_str <- paste(data_str_single_str, "&",
                                     data_str[i, j], sep = "")
      } else {
        data_str_single_str <- paste(data_str_single_str, "&",
                                     data_str[i, j], "\\\\\n", sep = "")
      }
    }
    if (i == num_dataset) {
      data_str_single_str <- paste(data_str_single_str, "\\midrule\n",
                                   data_avg_metric_str_0, data_avg_rank_str_0, sep = "")
    }
    if (i == (2*num_dataset)) {
      data_str_single_str <- paste(data_str_single_str, "\\midrule\n",
                                   data_avg_metric_str_25label, data_avg_rank_str_25label, sep = "")
    }
    if (i == (3*num_dataset)) {
      data_str_single_str <- paste(data_str_single_str, "\\midrule\n",
                                   data_avg_metric_str_25feature, data_avg_rank_str_25feature,
                                   "\\bottomrule\n", sep = "")
    }
  }
  write(data_str_single_str, file = paste("classification_res_txt/", kernel_type, "_res", idx_metric, ".txt", sep = ""))
}